package com.shyfu.dao;

import com.shyfu.entities.Comment;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by Svitlana on 04-Apr-14.
 */
@Repository
public class CommentDAOImpl implements CommentDAO {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public void addComment(Comment comment) {
        sessionFactory.getCurrentSession().save(comment);
    }

    @Override
    public void removeComment(Comment comment) {
        sessionFactory.getCurrentSession().delete(comment);
    }

    @Override
    public void removeComment(long id) {
        Comment comment = getComment(id);
        if (comment != null) {
            removeComment(comment);
        }
    }

    @Override
    public List<Comment> getAllComments() {
        return sessionFactory.getCurrentSession().createQuery("from Comment order by created").list();
    }

    @Override
    public Comment getComment(long id) {
        return (Comment) sessionFactory.getCurrentSession().load(Comment.class, id);
    }
}
